package com.jinyijia.api.rest.v1;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinyijia.api.rest.v1.dto.BannerDTO;
import com.jinyijia.api.rest.v1.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WX
 * @date 2018/8/10
 **/
@RestController
@RequestMapping("/ad")
public class AdController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/advert")
    public Map<String, Object> getAds() {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("banners", this.getBanners());
        map.put("advertisement", this.getRecommendedAds());
        map.put("floor", this.getFloor());
        map.put("popular", this.getPopular());
        return map;
    }

    /**
     * ---广告位(轮播图)
     *
     * @return
     */
    private BannerVO getBanners() {
        JSONArray jsonArray = restTemplate.getForEntity("http://content-service/ads?adsType=HOME_BANNER", JSONArray.class).getBody();
        List<BannerDTO> banners = new ArrayList<>();
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject job = jsonArray.getJSONObject(i);
                Long id = Long.valueOf(job.getString("url").substring(job.getString("url").indexOf("=") + 1));
                banners.add(new BannerDTO(id, job.getString("img"), job.getInteger("sortNum")));
            }
        }
        return new BannerVO(0, 12, banners);
    }

    /**
     * 组装楼层信息
     *
     * @return
     */
    private List<FloorVO> getFloor() {
        String url = "http://content-service/floor?show=" + true;
        JSONArray jsonArray = restTemplate.getForEntity(url, JSONArray.class).getBody();
        List<FloorVO> floorVOS = new ArrayList<>();
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject job = jsonArray.getJSONObject(i);
                FloorVO floorTitleVO = new FloorVO();
                floorTitleVO.setId(5);
                floorTitleVO.setFloorName(job.getString("floorTitle"));
                List<RightTagListVO> rightTagList = JSONArray.parseArray(job.getString("rightTagList"), RightTagListVO.class);
                floorTitleVO.setRightTag(rightTagList.get(1).getName());
                floorTitleVO.setSpanSize(12);
                floorTitleVO.setFloorAdvertVOS(this.getFloorAdvert(job.getInteger("id")));//获取各个楼层下的楼层广告
                floorTitleVO.setFloorGoodsVOS(this.getFloorGoods(job.getInteger("id")));  //获取各个楼层下的商品
                floorVOS.add(floorTitleVO);
            }
        }
        return floorVOS;
    }

    /**
     * 楼层广告
     *
     * @param
     */
    private List<FloorAdvertVO> getFloorAdvert(Integer id) {
        JSONObject jsonObject = restTemplate.getForEntity("http://content-service/floor/{1}", JSONObject.class, id).getBody();
        List<FloorContentVO> floorContentVOS = jsonObject.getJSONArray("floorContents").toJavaList(FloorContentVO.class);
        List<FloorAdvertVO> floorAdvertVOS = new ArrayList<>();
        floorContentVOS.forEach(fc -> {
            if (("ADVERT").equals(fc.getFloorType())) {
                floorAdvertVOS.add(new FloorAdvertVO(Long.valueOf(fc.getContent().substring(fc.getContent().indexOf("=") + 1)), fc.getImage(), 6));
            }
        });
        return floorAdvertVOS;
    }

    /**
     * 楼层商品
     *
     * @param
     */
    private List<FloorGoodsVO> getFloorGoods(Integer id) {
        JSONObject jsonObject = restTemplate.getForEntity("http://content-service/floor/{1}", JSONObject.class, id).getBody();
        List<FloorContentVO> floorContentVOS = jsonObject.getJSONArray("floorContents").toJavaList(FloorContentVO.class);
        List<FloorGoodsVO> floorGoodsVOS = new ArrayList<>();
        floorContentVOS.forEach(fc -> {
            FloorGoodsVO floorGoodsVO = new FloorGoodsVO();
            if (("GOODS").equals(fc.getFloorType())) {
                Long goodId = Long.valueOf(fc.getContent());
                floorGoodsVO.setId(goodId);
                JSONObject jsonObject1 = restTemplate.getForEntity("http://goods-service/goods/" + goodId, JSONObject.class).getBody();
                floorGoodsVO.setGoodsName(jsonObject1.getString("title"));
                floorGoodsVO.setPrice(jsonObject1.getDouble("price"));
                floorGoodsVO.setImageUrl(fc.getImage());
                floorGoodsVO.setSpanSize(4);
                floorGoodsVOS.add(floorGoodsVO);
            }
        });
        List<RightTagListVO> rightTagList = jsonObject.getJSONArray("rightTagList").toJavaList(RightTagListVO.class);
        if (rightTagList.size() > 0) {
            Integer brandId = Integer.valueOf(rightTagList.get(0).getUrl().substring(rightTagList.get(0).getUrl().indexOf("=") + 1));
            String goodsName = rightTagList.get(0).getName();
            floorGoodsVOS.add(new FloorGoodsVO(null, brandId, goodsName, null, rightTagList.get(0).getUrl(), 4));
        }
        return floorGoodsVOS;
    }

    /**
     * ---推荐广告位
     *
     * @return
     */
    private Map<String, Object> getRecommendedAds() {
        Map<String, Object> map = new LinkedHashMap<>();
        JSONArray jsonArray = restTemplate.getForEntity("http://content-service/ads?adsType=HOME_RECOMMENDED_ADS", JSONArray.class).getBody();
        List<RecommendedAds> recommendedAds = new ArrayList<>();
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject job = jsonArray.getJSONObject(i);
                Long id = Long.valueOf(job.getString("url").substring(job.getString("url").indexOf("=") + 1));
                recommendedAds.add(new RecommendedAds(job.getString("img"), id));
            }
        }
        map.put("spanSize", 12);
        map.put("pictures", recommendedAds);
        return map;
    }

    /**
     * 特价
     *
     * @return
     */
    private List<HotAds> getSpecial() {
        JSONArray jsonArray = restTemplate.getForObject("http://content-service/recommend?recommendType=SPECIAL", JSONArray.class);
        List<HotAds> hotAds = new ArrayList<>();
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject o = jsonArray.getJSONObject(i).getJSONObject("goods");
                Long id = Long.valueOf(o.getString("url").substring(o.getString("url").indexOf("=") + 1));
                hotAds.add(new HotAds(id, o.getString("img"), o.getString("title"), o.getDouble("price")));
            }
        }
        return hotAds;
    }

    /**
     * 热销
     *
     * @return
     */
    private List<HotAds> getHot() {
        JSONArray jsonArray = restTemplate.getForEntity("http://content-service/recommend?recommendType=HOT", JSONArray.class).getBody();
        List<HotAds> hotAds = new ArrayList<>();
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject o = jsonArray.getJSONObject(i).getJSONObject("goods");
                Long id = Long.valueOf(o.getString("url").substring(o.getString("url").indexOf("=") + 1));
                hotAds.add(new HotAds(id, o.getString("img"), o.getString("title"), o.getDouble("price")));
            }
        }
        return hotAds;
    }

    /**
     * 好评
     *
     * @return
     */
    private List<HotAds> getPraise() {
        JSONArray jsonArray = restTemplate.getForEntity("http://content-service/recommend?recommendType=PRAISE", JSONArray.class).getBody();
        List<HotAds> hotAds = new ArrayList<>();
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject o = jsonArray.getJSONObject(i).getJSONObject("goods");
                Long id = Long.valueOf(o.getString("url").substring(o.getString("url").indexOf("=") + 1));
                hotAds.add(new HotAds(id, o.getString("img"), o.getString("title"), o.getDouble("price")));
            }
        }
        return hotAds;
    }

    /**
     * 组装特价/热销/好评
     *
     * @return
     */
    private Map<String, Object> getPopular() {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("spanSize", 12);
        map.put("specialPrice", this.getSpecial());
        map.put("sellWell", this.getHot());
        map.put("favorableComment", this.getPraise());
        return map;
    }


}
